﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using BackupRestore.Logger;

namespace BackupRestore.Gui
{
    public partial class BackupRestoreUIController : Form
    {
        /// <summary>
        /// Reference to the controller
        /// </summary>
        internal BackupRestoreController Controller;
        private static string _className;

        private UserControl CurrentUserControl;

        private LinkedList <UserControl> UIStepSequence;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="Controller">Reference to the controller</param>
        public BackupRestoreUIController(BackupRestoreController Controller)
        {

            _className = MethodBase.GetCurrentMethod().DeclaringType.Name;

            string _methodName = MethodBase.GetCurrentMethod().Name;

            Logger.Logger.LogMethodEntry(_className, _methodName);


            this.Controller = Controller;
            UIStepSequence = new LinkedList<UserControl>();

            InitializeComponent();
            InitWindow();


            this.FormClosing += WindowCloseRequested;

            Logger.Logger.LogMethodExit(_className, _methodName);
        }

        /// <summary>
        /// Creates the first window when the program starts.
        /// </summary>
        private void InitWindow()
        {

            string _methodName = MethodBase.GetCurrentMethod().Name;
            Logger.Logger.LogMethodEntry(_className, _methodName);

            UserControl Control = new SelectPerformBackupRestorePanel(this);
            CurrentUserControl = Control;
            UIStepSequence.AddLast(Control);

            this.Controls.Add(Control);

            Logger.Logger.LogMethodExit(_className, _methodName);

        }

        /// <summary>
        /// Deletegate function.  Called when user requests to close the UI.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void WindowCloseRequested(object sender, EventArgs e)
        {
            string _methodName = MethodBase.GetCurrentMethod().Name;

            Logger.Logger.LogMethodEntry(_className, _methodName);

            Controller.WindowCloseRequested();

            Logger.Logger.LogMethodExit(_className, _methodName);
        }

        /// <summary>
        /// Called when user presses "Perform Backup" button.
        /// </summary>
        internal void PerformBackupButtonClicked()
        {
            string _methodName = MethodBase.GetCurrentMethod().Name;

            Logger.Logger.LogMethodEntry(_className, _methodName);

            this.Controls.RemoveAt(0);

            UserControl Control = new SelectBasicAdvancedBackupPanel(this);
            CurrentUserControl = Control;
            UIStepSequence.AddLast(Control);

            this.Controls.Add(Control);


            Logger.Logger.LogMethodExit(_className, _methodName);

        }

        /// <summary>
        /// Called when user presses "Perform Restore" button.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        internal void PerformRestoreButtonClicked()
        {
            string _methodName = MethodBase.GetCurrentMethod().Name;

            Logger.Logger.LogMethodEntry(_className, _methodName);

            Logger.Logger.LogMethodExit(_className, _methodName);

        }

        /// <summary>
        /// Called by UI components when user presses "Basic Backup" button
        /// </summary>
        internal void PerformBasicBackupButtonClicked()
        {
            string _methodName = MethodBase.GetCurrentMethod().Name;

            Logger.Logger.LogMethodEntry(_className, _methodName);


            string[] pathsToBackup = { Environment.GetEnvironmentVariable("USERPROFILE") };
            Controller.StartPrebackupAnalysis(pathsToBackup);

            Logger.Logger.LogMethodExit(_className, _methodName);
        }

        /// <summary>
        /// Called by UI components when user presses "Advanced Backup" button
        /// </summary>
        internal void PerformAdvancedBackupButtonClicked()
        {
            string _methodName = MethodBase.GetCurrentMethod().Name;

            Logger.Logger.LogMethodEntry(_className, _methodName);


            Logger.Logger.LogMethodExit(_className, _methodName);
        }
    }
}